using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MessagePack;

namespace Gj
{
    public enum Language:int { None = -1, Chinese = 0, English = 1, Japanese = 2, Korean = 3 }
    public class Localization
    {
        //单例模式    
        private static Localization _instance;

        public static Localization Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Localization();
                }

                return _instance;
            }
        }

        //选择自已需要的本地语言    
        public Language language = Language.Chinese;

        private Dictionary<string, string> dicA = new Dictionary<string, string>();
        private Dictionary<string, Dictionary<string, string>> dicB = new Dictionary<string, Dictionary<string, string>>();
        /// <summary>    
        /// 读取配置文件，将文件信息保存到字典里    
        /// </summary>    
        public Localization()
        {
            Set(Game.single.GetLanguage());
        }

        public static string[] GetLanguageList()
        {
            //, LanguageToString(Language.Japanese), LanguageToString(Language.Korean)
            return new[] { LanguageToString(Language.Chinese), LanguageToString(Language.English) };
        }

        public void Update(Dictionary<Language, string> res)
        {
            dicA = Cov(res[language]); 
        }

        public void Set(Language l)
        {
            language = l;
        }

        public void Set(string l)
        {
            Set(StringToLanguage(l));
        }

        public void Refresh(string l)
        {
            Set(l);
            Update();
        }

        public void Update()
        {
            var ta = Resource.GetLocalLanguage(LanguageLocalFileName(language));
            if (ta != null)
            {
                dicA = Cov(ta.text); 
            }
            var tb = Resource.GetOnlineLanguage(LanguageOnlineFileName(language));
            if (tb != null)
            {
                dicB = Parse(tb.bytes); 
            }
        }

        public string GetLanguageAbbreviation()
        {
            return LanguageAbbreviation(language);
        }

        public static Language StringToLanguage(string l)
        {
            switch (l)
            {
                case "中文":
                    return Language.Chinese;
                case "English":
                    return Language.English;
                case "日本語":
                    return Language.Japanese;
                case "한국어":
                    return Language.Korean;
            }

            return Language.Chinese;
        }

        public static string LanguageToString(Language l)
        {
            switch (l)
            {
                case Language.Chinese:
                    return "中文";
                case Language.English:
                    return "English";
                case Language.Japanese:
                    return "日本語";
                case Language.Korean:
                    return "한국어";
            }

            return "中文";
        }

        public static string LanguageLocalFileName(Language l)
        {
            switch (l)
            {
                case Language.Chinese:
                    return "Chinese";
                case Language.English:
                    return "English";
                case Language.Japanese:
                    return "Japanese";
                case Language.Korean:
                    return "Korean";
            }
            
            return "Chinese";
        }

        public static string LanguageOnlineFileName(Language l)
        {
            switch (l)
            {
                case Language.Chinese:
                    return "zh";
                case Language.English:
                    return "en";
                case Language.Japanese:
                    return "jp";
                case Language.Korean:
                    return "ko";
            }

            return "zh";
        }

        public static string LanguageAbbreviation(Language l)
        {
            switch (l)
            {
                case Language.Chinese:
                    return "zh";
                case Language.English:
                    return "en";
                case Language.Japanese:
                    return "jp";
                case Language.Korean:
                    return "ko";
            }

            return "zh";
        }

        /// <summary>    
        /// 获取value    
        /// </summary>    
        /// <param name="key"></param>    
        /// <returns></returns>    
        public string GetLocalValue(string key)
        {
            if (dicA.ContainsKey(key) == false)
            {
                return "";
            }
            return dicA[key];
        }

        /// <summary>    
        /// 获取value    
        /// </summary>    
        /// <param name="key"></param>    
        /// <returns></returns>    
        public string GetOnlineValue(string key)
        {
            if (!IsOnline(key)) key = MakeLocalFormat(key);
            string[] keys = ParseKey(key);
            if (keys == null)
            {
                return "";
            }
            if (dicB.ContainsKey(keys[0]) == false)
            {
                return "";
            }
            if (dicB[keys[0]].ContainsKey(keys[1]) == false)
            {
                return "";
            }
            return dicB[keys[0]][keys[1]];
        }

        bool IsOnline(string t)
        {
            return t.Contains(":") && !t.Contains(" ");
        }

        string MakeLocalFormat(string t)
        {
            return string.Format("language:{0}", t);
        }

        public static string GetLanguageString()
        {
            return LanguageToString(GetLanguage());
        }

        public static Language GetLanguage()
        {
            Language l = Language.Chinese;
            switch (Application.systemLanguage)
            {
                case SystemLanguage.Chinese:
                case SystemLanguage.ChineseTraditional:
                case SystemLanguage.ChineseSimplified:
                    l = Language.Chinese;
                    break;
                case SystemLanguage.Japanese:
                    l = Language.Japanese;
                    break;
                case SystemLanguage.Korean:
                    l = Language.Korean;
                    break;
                default:
                    l = Language.English;
                    break;
            }

            return l;
        }

        public static Dictionary<string, string> Cov(string text)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] lines = text.Split('\n');
            foreach (string line in lines)
            {
                if (line == null || line == "" || line.Length <= 1)
                {
                    continue;
                }
                string[] keyAndValue = line.Split(':');
                dic.Add(keyAndValue[0], keyAndValue[1].TrimStart().Trim('"'));
            }

            return dic;
        }

        public static Dictionary<string, Dictionary<string, string>> Parse(byte[] data)
        {
            
            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();
            object[] lines = MessagePackSerializer.Deserialize<object[]>(data);
            foreach (object[] line in lines)
            {
                string[] keys = ParseKey(line[0].ToString());
                if (keys == null)
                {
                    LogTools.Error("ParseKey Error: {0}", line[0].ToString());
                    continue;
                }
                if (!dic.ContainsKey(keys[0]))
                {
                    dic.Add(keys[0], new Dictionary<string, string>());
                }
                // Debug.LogErrorFormat("{0}=>{1} {2}", keys[0], keys[1], line[1]);
                dic[keys[0]].Add(keys[1], line[1].ToString());
            }

            return dic;
        }

        public static string[] ParseKey(string key)
        {
            string[] r = key.Split(':');
            if (r.Length < 2) return null;
            return new[] { r[0], r[1] };
        }
    }
}
